const util = require('../../utils/util.js');
const api = require('../../config/api.js');
Page({
    data: {
        list5ws3AYTQ: [],
        page: 1,
        totalPages: 1,
        meetingDocumentsUrl:''
    },
    onShareAppMessage() {
        return {};
    },
    view_18OnClick(e) {
        const id = e.currentTarget.dataset.id;
        wx.navigateTo({ url: '/pages/ActivityDetail/ActivityDetail?id='+id});
    },
    onLoad() {
        this.getIndexData();
    },
    getIndexData() {
        let that = this;
        util.request(api.ActivityApplyPage, {
            pageRequestVo: {
                pageNum: this.data.page,
                pageSize: 10
            }
        }, 'POST').then(function (res) {
            if (res.errno === 200) {
              if(res.data.total){
                let resList = res.data.list;
                resList.map(  //时间戳转换
                    val => {
                        val.createTime =util.formatTime(new Date(val.createTime));
                    }
                );
                that.setData({
                    list5ws3AYTQ:  that.data.list5ws3AYTQ.concat(resList),
                    totalPages: res.data.pages
                });
              }
                // let resList = res.data.list;
                // resList.map(  //时间戳转换
                //     val => {
                //         val.createTime =util.formatTime(new Date(val.createTime));
                //     }
                // );
                // that.setData({
                //     list5ws3AYTQ:  that.data.list5ws3AYTQ.concat(resList),
                //     totalPages: res.data.pages
                // });
            }
        });
    },
    onPullDownRefresh() {
        wx.stopPullDownRefresh() //停止下拉刷新
    },

    toUploadPdf(e){
      const that =this
      const id = e.currentTarget.dataset.id;
      //  console.log(id)
       util.request(api.ActivityDetail, {id: id}, 'get').then(function (res) {
        if (res.errno === 200) {
          that.setData({
                meetingDocumentsUrl: res.data.meetingDocumentsUrl
            })
            that.downloadAndOpenPDF()
        }
    });
          },
           // 定义一个函数用于下载并打开PDF文件
    downloadAndOpenPDF() {
        const that = this; // 确保此处的that指向正确

        if (!that.data.meetingDocumentsUrl == "") {

            wx.downloadFile({
                url: that.data.meetingDocumentsUrl,
                success: function (res) {
                    // console.log(that.data.meetingDocumentsUrl)
                    const filePath = res.tempFilePath;
                    wx.openDocument({
                        filePath: filePath,
                        showMenu: true
                    });
                    // 下载完成后，可以选择是否执行其他操作，比如返回上一页
                    // wx.navigateBack({
                    //     delta: 1
                    // });
                },
                fail: function (err) {
                    wx.showToast({
                        title: '文件下载失败，请重试',
                        icon: 'none',
                        duration: 2000
                    });
                    // 可能需要根据实际情况处理下载失败的情况
                }
            });

        }
    },

    onReachBottom() {
        if (this.data.page < this.data.totalPages) {
            this.setData({
                page: this.data.page + 1
            });
            this.getIndexData();
        } else {
            wx.showToast({
                title: '没有更多了',
                icon: 'none',
                duration: 2000
            });
            return false;
        }
    },

});
